package com.jxwifi.cloud.quickclean.toolkit;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.BuyToolKitAdapter;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.MyCleanersBean;
import com.jxwifi.cloud.quickclean.bean.RecordsListBean;
import com.jxwifi.cloud.quickclean.bean.ShopPurchaseBean;
import com.jxwifi.cloud.quickclean.bean.ShopPurchaseDtoBean;
import com.jxwifi.cloud.quickclean.bean.ToolKitBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.popwindow.PopOrderStartModule;
import com.jxwifi.cloud.quickclean.popwindow.PopShopDetails;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.PopWindowUtil;
import com.popwindow.Popwindows;

import java.util.ArrayList;
import java.util.List;

public class BuyToolKitPresenter {

    private Context mContext;
    private BuyToolKitView mView;
    private AuzImpl mAuz;

    private BuyToolKitAdapter mAdapter;
    private double mAllPrice; //总价格
    private boolean mIsAllCHeck = true;//是否全选
    private List<RecordsListBean> mrecords;

    private List<ShopPurchaseBean> mShopPur = new ArrayList<>();


    public BuyToolKitPresenter(Context context, BuyToolKitView view) {
        this.mContext = context;
        this.mView = view;
        mAuz = new AuzImpl();
    }

    public void initView() {
        mView.getRvList().setLayoutManager(new LinearLayoutManager(mContext));
        mView.getRvList().setAdapter(mAdapter = new BuyToolKitAdapter(R.layout.list_buy_tool_kit_item));
        mAdapter.openLoadAnimation();
        mAdapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    //勾选
                    case R.id.cb_items_child:
                        CheckBox cbx = (CheckBox) view.findViewById(R.id.cb_items_child);
                        if (mAdapter.getData().get(position).getTotal() == 0) { //判断库存为0的时候提示用户
                            Toast toast = Toast.makeText(mContext, "库存已无货，请联系客服", Toast.LENGTH_SHORT);
                            ToastUtils.showMyToast(toast, 3000);
                            cbx.setChecked(false);
                        } else {
                            if (cbx.isChecked()) {
                                mAdapter.getData().get(position).setCheckbox(true);
                            } else {
                                mAdapter.getData().get(position).setCheckbox(false);
                            }
                        }
                        CalculationPrice();
                        break;
                    //减少数量
                    case R.id.tv_buy_tool_kit_item_reduce:
                        if (mAdapter.getData().get(position).getTotal() == 0) { //判断库存为0的时候提示用户
                            Toast toast = Toast.makeText(mContext, "库存已无货，请联系客服", Toast.LENGTH_SHORT);
                            ToastUtils.showMyToast(toast, 3000);
                        } else {
                            if (mAdapter.getData().get(position).getToolnum() == 1) {
                                Toast toast = Toast.makeText(mContext, "数量已是最小值", Toast.LENGTH_SHORT);
                                ToastUtils.showMyToast(toast, 3000);
                            } else {
                                int mToolNum = mAdapter.getData().get(position).getToolnum();
                                mAdapter.getData().get(position).setToolnum(mToolNum - 1);
                                CalculationPrice();
                            }
                        }
                        break;
                    //增加数量
                    case R.id.tv_buy_tool_kit_item_plus:
                        if (mAdapter.getData().get(position).getTotal() == 0) { //判断库存为0的时候提示用户
                            Toast toast = Toast.makeText(mContext, "库存已无货，请联系客服", Toast.LENGTH_SHORT);
                            ToastUtils.showMyToast(toast, 3000);
                        } else {
                            if (mAdapter.getData().get(position).getToolnum() == mAdapter.getData().get(position).getTotal()) {
                                Toast toast = Toast.makeText(mContext, "数量已是最大值", Toast.LENGTH_SHORT);
                                ToastUtils.showMyToast(toast, 3000);
                            } else {
                                int mToolNum = mAdapter.getData().get(position).getToolnum();
                                mAdapter.getData().get(position).setToolnum(mToolNum + 1);
                                CalculationPrice();
                            }
                        }

                        break;
                    //展示商品详情
                    case R.id.rel_buy_tool_kit:
//                        Toast toast = Toast.makeText(mContext, "展示商品详情", Toast.LENGTH_SHORT);
//                        ToastUtils.showMyToast(toast, 3000);
                        ShopDetails(mAdapter.getData().get(position));
                        break;
                }
            }
        });

        initData();
    }

    public void initData() {
        Params params = new Params();
        mAuz.auzget(UrlPool.GET_SHOP_LIST, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                //RecordsListBean
                if (code == 0) {
                    mrecords = JSON.parseArray(data, RecordsListBean.class);
                    if (mrecords.size() != 0 || mrecords != null) {
                        mAdapter.setNewData(mrecords);
                    } else {
                        mAdapter.setEmptyView(mView.getViews());
                    }
                } else {
                    mView.oninitData();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {

                mView.onFailure(errorCode, msg);
            }
        });
    }

    //价格计算
    public void CalculationPrice() {
        mIsAllCHeck = true;
        mAllPrice = 0.0;
        mShopPur.clear();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            if (mAdapter.getData().get(i).isCheckbox()) {
                mAllPrice = mAllPrice + mAdapter.getData().get(i).getToolnum() * mAdapter.getData().get(i).getDiscountValue();
                //获取选中数据中的ID与数量做结算接口请求
                ShopPurchaseBean mBean = new ShopPurchaseBean();
                mBean.setNumber(mAdapter.getData().get(i).getToolnum());
                mBean.setSn(mAdapter.getData().get(i).getSn());
                mShopPur.add(mBean);
            } else {
                mIsAllCHeck = false;
            }
        }
        mView.getAllPrice().setText("\u00A5" + mAllPrice);
        mView.getChild().setChecked(mIsAllCHeck);
        mAdapter.notifyDataSetChanged();
    }

    //全选
    public void AllCheck() {
        if (mView.getChild().isChecked()) {
            // mView.getChild().setChecked(true);
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                if (mAdapter.getData().get(i).getTotal() > 0) {
                    mAdapter.getData().get(i).setCheckbox(true);
                } else {
                    mAdapter.getData().get(i).setCheckbox(false);
                }
            }
        } else {
            //mView.getChild().setChecked(false);
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                mAdapter.getData().get(i).setCheckbox(false);
            }

        }
        CalculationPrice();
    }

    //结算
    public void Settlement() {
        if (mShopPur.size() == 0) {
            Toast toast = Toast.makeText(mContext, "请选择您要结算的商品", Toast.LENGTH_SHORT);
            ToastUtils.showMyToast(toast, 3000);
            return;
        }
        Params params = new Params();
        params.add("shopPurchase", mShopPur);
        mAuz.auzpost(UrlPool.POST_SHOP_PURCHASE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    ShopPurchaseDtoBean mBean = JSON.parseObject(data, ShopPurchaseDtoBean.class);
                    Intent intent = new Intent();
                    intent.setClass(mContext, TKConfirmActivity.class);
                    intent.putExtra("shoppur", mBean);
                    mContext.startActivity(intent);
                } else {

                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //商品详情
    public void ShopDetails(RecordsListBean mRecords) {

        PopShopDetails module = new PopShopDetails(mContext, mRecords);
        Popwindows window = PopWindowUtil.getPopupWindow(module.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT);
        module.setPopwindow(window);
        window.show(mView.getSettlement(), Popwindows.Location.TOP);

    }
}
